<template>
  <!-- 派单/接单 -->
  <a-modal
    :width="500"
    :visible="dispatchVisible"
    @ok="handleDispatchSubmit"
    @cancel="handleDispatchClose"
    cancelText="关闭"
    title="派单"
  >
    <a-spin :spinning="confirmLoading">
      <a-form layout="horizontal">
        <a-row :gutter="24">
          <a-col>
            <a-form-model-item
              label="工作组："
              :label-col="{ span: 5 }"
              :wrapper-col="{ span: 16 }"
              prop="workGroup"
              :rules="$valid.required"
            >
              <a-tree-select
                v-model="dispatch.workGroups"
                style="width: 100%"
                tree-checkable
                :treeCheckStrictly="true"
                allow-clear
                :tree-data="workGroupTree"
                placeholder="请选择工作组"
                @change="forceUpdate"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col>
            <a-form-model-item
              label="人员："
              :label-col="{ span: 5 }"
              :wrapper-col="{ span: 16 }"
              prop="maintenanceManager"
              :rules="$valid.required"
            >
              <a-select
                v-model="dispatch.maintenanceManager"
                placeholder="请选择人员"
                class="my-select"
                @change="forceUpdate"
              >
                <a-select-option
                  v-for="item in createByList"
                  :key="item.companyId + ''"
                  :value="item.companyId + ''"
                >
                  {{ item.companyName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row :gutter="24">
          <a-col>
            <a-form-model-item
              label="备注："
              :label-col="{ span: 5 }"
              :wrapper-col="{ span: 16 }"
              prop="dispatch"
            >
              <a-input v-model="dispatch.dispatchRemarks" type="textarea" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import moment from 'moment'
import { getAction, postAction, deleteAction } from '@/api/manage'
import { getDepartTree, getCompanyList } from '@/api/api'
import WImageUpload from '@/components/WxImageUpload/WImageUpload'
import WFileUpload from '@/components/WxFileUpload/WFileUpload.vue'

export default {
  name: 'DispatchForm',
  components: {
    WImageUpload,
    WFileUpload,
  },
  computed: {},
  data() {
    return {
      confirmLoading: false,
      workGroupTree: [],
      dispatchVisible: false,
      dispatch: {
        ids: undefined,
        workGroup: undefined,
        maintenanceManager: undefined,
        dispatchRemarks: undefined,
      },

      current: 0,
      url: {
        update: '/system/devMaintenance/update',
        getWorkGroupTree: '/system/devMaintenanceWorkGroup/tree',
      },

      createByList: [],
    }
  },
  created() {
    this.getUserList()
    this.getWorkGroup()
  },
  methods: {
    forceUpdate() {
      this.$forceUpdate()
    },

    getCreateTime(createTime) {
      if (createTime) {
        return createTime.replace('T', ' ')
      }
    },
    getDictName(value, dictType) {
      if (value) {
        var dictList = this.$sysDictOptions(dictType)
        return dictList[`wx${value}`].v
      }
      return '--'
    },

    //工作组
    getWorkGroup() {
      getAction(this.url.getWorkGroupTree).then((res) => {
        this.workGroupTree = res.result.data
      })
    },

    getUserList() {
      // 加载数据方法 必须为 Promise 对象
      var parameter = { pageNo: 1, pageSize: 1000000 }
      getCompanyList(Object.assign(parameter, this.queryParam)).then((res) => {
        this.createByList = res.result.data.records
      })
    },
    getCreateByName(text) {
      var obj = this.createByList.filter(
        (aa) => aa.companyId + '' == text + ''
      )[0]
      if (obj == null) {
        if (text) {
          return '系统管理员'
        } else {
          return '--'
        }
      }
      return obj.companyName
    },
    moment,

    //START 派单
    handleDispatch(dispatchType, id) {
      console.log('id>>>>', id)
      this.dispatchVisible = true
      this.dispatch = {}
      this.dispatch.dispatchType = dispatchType
      this.dispatch.ids = id
    },

    handleDispatchSubmit() {
      if (!this.dispatch.workGroups) {
        this.$message.error('请选择工作组')
        return
      }

      if (!this.dispatch.maintenanceManager) {
        this.$message.error('请选择人员')
        return
      }

      this.dispatch.workGroup = JSON.stringify(this.dispatch.workGroups)

      this.dispatchVisible = true
      this.dispatch.workOrderStatus = '1'
      postAction(this.url.update, this.dispatch).then((resp) => {
        if (this.dispatch.dispatchType == '0') {
          this.$message.success('派单成功')
        } else {
          this.$message.success('转派成功')
        }
        this.handleDispatchClose()
      })
    },
    handleDispatchClose() {
      this.dispatchVisible = false
      this.dispatch = {}
      this.$emit('close')
    },
    //END 派单
  },
}
</script>
<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC',
    'Microsoft YaHei', sans-serif;
  background-color: #f0f2f5;
  color: #333;
  padding: 20px;
}
</style>
